package com.gimi.test_jpa.com.gimi.config;

import com.gimi.test_jpa.com.gimi.annotation.ResultInterceptor;
import com.gimi.test_jpa.com.gimi.aop.TransCacheInfoUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */
@Component
@CrossOrigin
public class MyIntercetpr implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        response.setCharacterEncoding("utf-8");
        System.out.println(handler+ ">>>>>>>>>>>>>>>>进入了拦截器");

        String[] split = handler.toString().split("#");

        if (split[0].startsWith("com.gimi.test_jpa.com.gimi")){
            Class<?> clazz = Class.forName(split[0]);
//            String met = split[1].split("\\(")[0];
//            Method[] methods = clazz.getDeclaredMethods();
//            for (Method method : methods) { //拦截处理指定方法
//                if ("getStr".equals(method.getName())&&met.equals(method.getName())) {
//                    Object res = method.invoke(clazz.newInstance(), "天气");
//                    response.getWriter().write(res.toString());
//                    return false;
//                }
//            }
            if (clazz.isAnnotationPresent(ResultInterceptor.class)) {
                TransCacheInfoUtil.setResultInterceptor(true);
            }
        }
        return true;
    }
}
